<template>
  <div class="login">
    <div class="left">
      <div class="header">
        <img class="title" src="@/assets/img/login_logo.png" alt="" />
        <span class="title-name">黑马面面</span>
        <div class="title-shuxian"></div>
        <span class="title-yonghu">用户登陆</span>
      </div>
      <el-form
        class="form"
        :model="form"
        :rules="rules"
        ref="form"
        label-width="0px"
      >
        <el-form-item prop="phone">
          <el-input
            placeholder="请输入手机号"
            v-model="form.phone"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="form.password"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input
                placeholder="请输入验证码"
                v-model="form.code"
                prefix-icon="el-icon-key"
              ></el-input>
            </el-col>
            <el-col :span="8">
              <img
                class="code-img"
                @click="refreshCode"
                :src="codeUrl"
                alt=""
              />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass" :label="true"
            >我已阅读并同意<el-link type="primary">用户协议</el-link>和<el-link
              type="primary"
              >隐私条款</el-link
            ></el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="login">登陆</el-button>
          <div></div>
          <el-button type="primary" class="btn m26" @click="zhuce"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <img src="@/assets/img/login_banner_ele.png" alt="" />
    <Add v-model="isShow" />
  </div>
</template>
<script>
// import { getTokena } from '@/utils/save.js'
import { toLogin } from '@/api/login.js'
import Add from './component/add.vue'
export default {
  components: {
    Add
  },
  data () {
    return {
      isShow: false,
      codeUrl: process.env.VUE_APP_URL + '/captcha?type=login',
      form: {
        phone: '18511111111',
        password: '12345678',
        code: '',
        isPass: []
      },

      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (_reg.test(value)) {
                callback()
              } else {
                callback(new Error('请正确输入手机号'))
              }
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          {
            min: 6,
            max: 12,
            message: '请输入6-12位密码',
            trigger: 'change'
          }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'change' },
          {
            min: 4,
            max: 4,
            message: '请输入正确验证码'
          }
        ],
        isPass: [{ required: true, message: '请勾选协议', trigger: 'change' }]
      }
    }
  },
  created () {
    // if (getTokena()) {
    //   this.$router.push('/layout')
    // }
    console.log(this.codeUrl)
  },
  methods: {
    login () {
      this.$refs.form.validate(async result => {
        if (result) {
          // await this.$store.dispatch('login/tolayout', this.form)
          toLogin(this.form).then(res => {
            if (res.data.code === 200) {
              this.$message.success('登陆成功')
              this.$store.commit('login/setToken', res.data.data.token, {
                root: true
              })
              this.$router.push('/layout/dataOverview')
            } else {
              this.$message.error(res.data.message + '错误')
            }
          })
        } else {
          this.$message.error('验证失败')
        }
      })
    },
    refreshCode () {
      this.codeUrl =
        process.env.VUE_APP_URL + '/captcha?type=login&dfhdfh=' + Date.now()
    },
    zhuce () {
      this.isShow = true
      // this.isShow = data
    }
  }
}
</script>
<style lang="less">
.login {
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;

  align-items: center;
  justify-content: space-around;
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 44px 42px;
    .el-link.el-link--primary {
      margin-top: -2px;
    }
    .header {
      display: flex;

      align-items: center;
      .title {
        width: 25px;
      }
      .title-name {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin-left: 13px;
        margin-right: 13px;
      }
      .title-shuxian {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin-right: 13px;
      }
      .title-yonghu {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }

    .form {
      margin-top: 31px;
      .code-img {
        width: 100%;
        height: 40px;
      }
      .btn {
        width: 100%;
      }
      .m26 {
        margin-top: 26px;
      }
    }
  }
}
</style>
